﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace QuanLyPhongKham
{
	public class frmHoantrancc : BaseUserControl
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Label label9;
		private LibList.List listDMBD;//=new LibList.List();
		private System.Windows.Forms.Label label13;
		private System.Windows.Forms.Label lTen;
		private System.Windows.Forms.Label label16;
		private System.Windows.Forms.Label label17;
		private System.Windows.Forms.Label label18;
		private System.Windows.Forms.Label label20;
		private System.Windows.Forms.TextBox tenbd;
        private System.Windows.Forms.TextBox mabd;
		private System.Windows.Forms.ComboBox cmbSophieu;
		private string s_mmyy,s_ngay,sql,s_loai,s_ngaysp,user;
		private int i_userid,i_mabd;
		private long l_id,l_sttt,l_idduyet;
		private decimal d_soluong,d_dongia,d_sotien,d_giaban,d_soluongton,d_soluongcu,d_tongcong;
        private bool bNew, bEdit = true;
		private DAL.AccessData d;
		private DataTable dtton=new DataTable();
		private DataTable dtll=new DataTable();
		private DataTable dtct=new DataTable();
		private DataTable dtxoa=new DataTable();
		private DataTable dtdmnx=new DataTable();
		private System.Windows.Forms.Label ldvt;
		private DataRow r;
		private System.Windows.Forms.Label label15;
        private System.Windows.Forms.Label label24;
		private System.Windows.Forms.TextBox tenhc;
        private System.Windows.Forms.Label lTenhc;
		private System.Windows.Forms.Label label25;
        private System.Windows.Forms.ComboBox khox;
        private System.Windows.Forms.ComboBox manguon;
		private System.Windows.Forms.TextBox stt;
		private System.Windows.Forms.TextBox sttt;
        private System.Windows.Forms.TextBox timkiem;
		private System.Windows.Forms.TextBox tendv;
		private System.Windows.Forms.TextBox madv;
		private System.Windows.Forms.Label label5;
        private DataGrid dataGrid1;
        private Panel bNut;
        private Button butKetthuc;
        private Button butChon;
        private Button butHuy;
        private Button butSua;
        private Button butMoi;
        private Button butLuu;
        private Button butIn;
        private Button butBoqua;
        private Label label3;
        private TextBox lydo;
        private Button butXoa;
        private Button butTonghop;
        private TextBox losx;
        private TextBox handung;
        private TextBox soluong;
        private TextBox dongia;
        private TextBox giaban;
        private TextBox sotien;
        private TextBox dvt;
        private TextBox sophieu;
        private System.Windows.Forms.MaskedTextBox ngayhd;
        private Button butThem;
		private System.ComponentModel.Container components = null;

		public frmHoantrancc(DAL.AccessData acc,string loai,string mmyy,string ngay,int userid)
		{
			InitializeComponent();
            d = acc; i_userid = userid; s_mmyy = mmyy; s_ngay = ngay; s_loai = loai;
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.label9 = new System.Windows.Forms.Label();
            this.khox = new System.Windows.Forms.ComboBox();
            this.listDMBD = new LibList.List();
            this.label13 = new System.Windows.Forms.Label();
            this.lTen = new System.Windows.Forms.Label();
            this.ldvt = new System.Windows.Forms.Label();
            this.label16 = new System.Windows.Forms.Label();
            this.label17 = new System.Windows.Forms.Label();
            this.label18 = new System.Windows.Forms.Label();
            this.label20 = new System.Windows.Forms.Label();
            this.tenbd = new System.Windows.Forms.TextBox();
            this.mabd = new System.Windows.Forms.TextBox();
            this.cmbSophieu = new System.Windows.Forms.ComboBox();
            this.label15 = new System.Windows.Forms.Label();
            this.label24 = new System.Windows.Forms.Label();
            this.tenhc = new System.Windows.Forms.TextBox();
            this.lTenhc = new System.Windows.Forms.Label();
            this.label25 = new System.Windows.Forms.Label();
            this.manguon = new System.Windows.Forms.ComboBox();
            this.stt = new System.Windows.Forms.TextBox();
            this.sttt = new System.Windows.Forms.TextBox();
            this.timkiem = new System.Windows.Forms.TextBox();
            this.tendv = new System.Windows.Forms.TextBox();
            this.madv = new System.Windows.Forms.TextBox();
            this.label5 = new System.Windows.Forms.Label();
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.bNut = new System.Windows.Forms.Panel();
            this.butThem = new System.Windows.Forms.Button();
            this.butXoa = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butChon = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butMoi = new System.Windows.Forms.Button();
            this.butLuu = new System.Windows.Forms.Button();
            this.butIn = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.lydo = new System.Windows.Forms.TextBox();
            this.butTonghop = new System.Windows.Forms.Button();
            this.losx = new System.Windows.Forms.TextBox();
            this.handung = new System.Windows.Forms.TextBox();
            this.soluong = new System.Windows.Forms.TextBox();
            this.dongia = new System.Windows.Forms.TextBox();
            this.giaban = new System.Windows.Forms.TextBox();
            this.sotien = new System.Windows.Forms.TextBox();
            this.dvt = new System.Windows.Forms.TextBox();
            this.sophieu = new System.Windows.Forms.TextBox();
            this.ngayhd = new System.Windows.Forms.MaskedTextBox();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.bNut.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(133, 6);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(78, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Số hoá đơn :";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label2
            // 
            this.label2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.Location = new System.Drawing.Point(6, 6);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(75, 23);
            this.label2.TabIndex = 1;
            this.label2.Text = "Ngày nhập :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label9
            // 
            this.label9.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label9.Location = new System.Drawing.Point(322, 6);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(64, 23);
            this.label9.TabIndex = 8;
            this.label9.Text = "Kho xuất : ";
            this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // khox
            // 
            this.khox.BackColor = System.Drawing.SystemColors.HighlightText;
            this.khox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.khox.Enabled = false;
            this.khox.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.khox.Location = new System.Drawing.Point(381, 6);
            this.khox.Name = "khox";
            this.khox.Size = new System.Drawing.Size(114, 21);
            this.khox.TabIndex = 3;
            this.khox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.khox_KeyDown);
            // 
            // listDMBD
            // 
            this.listDMBD.BackColor = System.Drawing.SystemColors.Info;
            this.listDMBD.ColumnCount = 0;
            this.listDMBD.Location = new System.Drawing.Point(208, 230);
            this.listDMBD.MatchBufferTimeOut = 1000;
            this.listDMBD.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
            this.listDMBD.Name = "listDMBD";
            this.listDMBD.Size = new System.Drawing.Size(75, 17);
            this.listDMBD.TabIndex = 26;
            this.listDMBD.TextIndex = -1;
            this.listDMBD.TextMember = null;
            this.listDMBD.ValueIndex = -1;
            this.listDMBD.Visible = false;
            this.listDMBD.KeyDown += new System.Windows.Forms.KeyEventHandler(this.listDMBD_KeyDown);
            // 
            // label13
            // 
            this.label13.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label13.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label13.Location = new System.Drawing.Point(20, 337);
            this.label13.Name = "label13";
            this.label13.Size = new System.Drawing.Size(61, 23);
            this.label13.TabIndex = 28;
            this.label13.Text = "Mã thuốc :";
            this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lTen
            // 
            this.lTen.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.lTen.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lTen.Location = new System.Drawing.Point(141, 337);
            this.lTen.Name = "lTen";
            this.lTen.Size = new System.Drawing.Size(65, 23);
            this.lTen.TabIndex = 29;
            this.lTen.Text = "Tên thuốc :";
            this.lTen.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // ldvt
            // 
            this.ldvt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.ldvt.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ldvt.Location = new System.Drawing.Point(530, 336);
            this.ldvt.Name = "ldvt";
            this.ldvt.Size = new System.Drawing.Size(42, 23);
            this.ldvt.TabIndex = 30;
            this.ldvt.Text = "ĐVT :";
            this.ldvt.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label16
            // 
            this.label16.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label16.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label16.Location = new System.Drawing.Point(25, 360);
            this.label16.Name = "label16";
            this.label16.Size = new System.Drawing.Size(56, 23);
            this.label16.TabIndex = 31;
            this.label16.Text = "Số lượng :";
            this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label17
            // 
            this.label17.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label17.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label17.Location = new System.Drawing.Point(150, 359);
            this.label17.Name = "label17";
            this.label17.Size = new System.Drawing.Size(56, 23);
            this.label17.TabIndex = 32;
            this.label17.Text = "Đơn giá :";
            this.label17.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label18
            // 
            this.label18.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label18.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label18.Location = new System.Drawing.Point(471, 359);
            this.label18.Name = "label18";
            this.label18.Size = new System.Drawing.Size(56, 23);
            this.label18.TabIndex = 33;
            this.label18.Text = "Số tiền :";
            this.label18.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label20
            // 
            this.label20.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label20.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label20.Location = new System.Drawing.Point(17, 382);
            this.label20.Name = "label20";
            this.label20.Size = new System.Drawing.Size(64, 23);
            this.label20.TabIndex = 35;
            this.label20.Text = "Nguồn :";
            this.label20.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tenbd
            // 
            this.tenbd.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tenbd.BackColor = System.Drawing.SystemColors.HighlightText;
            this.tenbd.Enabled = false;
            this.tenbd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tenbd.Location = new System.Drawing.Point(203, 336);
            this.tenbd.Name = "tenbd";
            this.tenbd.Size = new System.Drawing.Size(116, 21);
            this.tenbd.TabIndex = 7;
            this.tenbd.TextChanged += new System.EventHandler(this.tenbd_TextChanged);
            this.tenbd.Validated += new System.EventHandler(this.tenbd_Validated);
            this.tenbd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.tenbd_KeyDown);
            // 
            // mabd
            // 
            this.mabd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.mabd.BackColor = System.Drawing.SystemColors.HighlightText;
            this.mabd.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.mabd.Enabled = false;
            this.mabd.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.mabd.Location = new System.Drawing.Point(78, 336);
            this.mabd.Name = "mabd";
            this.mabd.Size = new System.Drawing.Size(57, 21);
            this.mabd.TabIndex = 6;
            this.mabd.TextChanged += new System.EventHandler(this.mabd_TextChanged);
            this.mabd.Validated += new System.EventHandler(this.mabd_TextChanged);
            this.mabd.KeyDown += new System.Windows.Forms.KeyEventHandler(this.mabd_KeyDown);
            // 
            // cmbSophieu
            // 
            this.cmbSophieu.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cmbSophieu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.cmbSophieu.Location = new System.Drawing.Point(208, 6);
            this.cmbSophieu.Name = "cmbSophieu";
            this.cmbSophieu.Size = new System.Drawing.Size(91, 21);
            this.cmbSophieu.TabIndex = 0;
            this.cmbSophieu.SelectedIndexChanged += new System.EventHandler(this.cmbSophieu_SelectedIndexChanged);
            this.cmbSophieu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cmbSophieu_KeyDown);
            // 
            // label15
            // 
            this.label15.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label15.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label15.Location = new System.Drawing.Point(204, 382);
            this.label15.Name = "label15";
            this.label15.Size = new System.Drawing.Size(61, 23);
            this.label15.TabIndex = 61;
            this.label15.Text = "Hạn dùng :";
            this.label15.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label24
            // 
            this.label24.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label24.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label24.Location = new System.Drawing.Point(313, 383);
            this.label24.Name = "label24";
            this.label24.Size = new System.Drawing.Size(80, 23);
            this.label24.TabIndex = 62;
            this.label24.Text = "Lô sản xuất :";
            this.label24.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tenhc
            // 
            this.tenhc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.tenhc.BackColor = System.Drawing.SystemColors.HighlightText;
            this.tenhc.Enabled = false;
            this.tenhc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tenhc.Location = new System.Drawing.Point(391, 336);
            this.tenhc.Name = "tenhc";
            this.tenhc.Size = new System.Drawing.Size(134, 21);
            this.tenhc.TabIndex = 8;
            // 
            // lTenhc
            // 
            this.lTenhc.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.lTenhc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lTenhc.Location = new System.Drawing.Point(332, 337);
            this.lTenhc.Name = "lTenhc";
            this.lTenhc.Size = new System.Drawing.Size(61, 23);
            this.lTenhc.TabIndex = 64;
            this.lTenhc.Text = "Hoạt chất :";
            this.lTenhc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // label25
            // 
            this.label25.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.label25.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label25.Location = new System.Drawing.Point(337, 360);
            this.label25.Name = "label25";
            this.label25.Size = new System.Drawing.Size(56, 23);
            this.label25.TabIndex = 66;
            this.label25.Text = "Giá bán :";
            this.label25.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // manguon
            // 
            this.manguon.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.manguon.BackColor = System.Drawing.SystemColors.HighlightText;
            this.manguon.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.manguon.Enabled = false;
            this.manguon.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.manguon.Location = new System.Drawing.Point(78, 382);
            this.manguon.Name = "manguon";
            this.manguon.Size = new System.Drawing.Size(126, 21);
            this.manguon.TabIndex = 13;
            // 
            // stt
            // 
            this.stt.Location = new System.Drawing.Point(58, 245);
            this.stt.Name = "stt";
            this.stt.Size = new System.Drawing.Size(24, 20);
            this.stt.TabIndex = 68;
            // 
            // sttt
            // 
            this.sttt.Location = new System.Drawing.Point(90, 245);
            this.sttt.Name = "sttt";
            this.sttt.Size = new System.Drawing.Size(24, 20);
            this.sttt.TabIndex = 69;
            // 
            // timkiem
            // 
            this.timkiem.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.timkiem.BackColor = System.Drawing.Color.White;
            this.timkiem.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.timkiem.Location = new System.Drawing.Point(496, 29);
            this.timkiem.Name = "timkiem";
            this.timkiem.Size = new System.Drawing.Size(131, 21);
            this.timkiem.TabIndex = 106;
            this.timkiem.Text = "Tìm kiếm";
            this.timkiem.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.timkiem.TextChanged += new System.EventHandler(this.find_TextChanged);
            this.timkiem.Enter += new System.EventHandler(this.find_Enter);
            // 
            // tendv
            // 
            this.tendv.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.tendv.BackColor = System.Drawing.SystemColors.HighlightText;
            this.tendv.Enabled = false;
            this.tendv.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tendv.Location = new System.Drawing.Point(143, 29);
            this.tendv.Name = "tendv";
            this.tendv.Size = new System.Drawing.Size(352, 21);
            this.tendv.TabIndex = 5;
            // 
            // madv
            // 
            this.madv.BackColor = System.Drawing.SystemColors.HighlightText;
            this.madv.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.madv.Enabled = false;
            this.madv.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.madv.Location = new System.Drawing.Point(78, 29);
            this.madv.Name = "madv";
            this.madv.Size = new System.Drawing.Size(64, 21);
            this.madv.TabIndex = 4;
            this.madv.KeyDown += new System.Windows.Forms.KeyEventHandler(this.madv_KeyDown);
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label5.Location = new System.Drawing.Point(2, 34);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(79, 13);
            this.label5.TabIndex = 111;
            this.label5.Text = "Nhà cung cấp :";
            this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.Color.White;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.Color.White;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.CaptionVisible = false;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(1, 53);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.RowHeaderWidth = 5;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(626, 278);
            this.dataGrid1.TabIndex = 113;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            // 
            // bNut
            // 
            this.bNut.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.bNut.Controls.Add(this.butThem);
            this.bNut.Controls.Add(this.butXoa);
            this.bNut.Controls.Add(this.butKetthuc);
            this.bNut.Controls.Add(this.butChon);
            this.bNut.Controls.Add(this.butHuy);
            this.bNut.Controls.Add(this.butSua);
            this.bNut.Controls.Add(this.butMoi);
            this.bNut.Controls.Add(this.butLuu);
            this.bNut.Controls.Add(this.butIn);
            this.bNut.Controls.Add(this.butBoqua);
            this.bNut.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.bNut.Location = new System.Drawing.Point(1, 433);
            this.bNut.Name = "bNut";
            this.bNut.Size = new System.Drawing.Size(626, 34);
            this.bNut.TabIndex = 114;
            this.bNut.SizeChanged += new System.EventHandler(this.bNut_SizeChanged);
            // 
            // butThem
            // 
            this.butThem.Image = global::QuanLyPhongKham.Properties.Resources.butThem;
            this.butThem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThem.Location = new System.Drawing.Point(189, 1);
            this.butThem.Name = "butThem";
            this.butThem.Size = new System.Drawing.Size(63, 31);
            this.butThem.TabIndex = 62;
            this.butThem.Text = "     &Thêm";
            this.butThem.UseVisualStyleBackColor = true;
            this.butThem.Click += new System.EventHandler(this.butThem_Click);
            // 
            // butXoa
            // 
            this.butXoa.Image = global::QuanLyPhongKham.Properties.Resources.butXoa2;
            this.butXoa.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butXoa.Location = new System.Drawing.Point(252, 1);
            this.butXoa.Name = "butXoa";
            this.butXoa.Size = new System.Drawing.Size(62, 31);
            this.butXoa.TabIndex = 61;
            this.butXoa.Text = "     &Xóa";
            this.butXoa.UseVisualStyleBackColor = true;
            this.butXoa.Click += new System.EventHandler(this.butXoa_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKetthuc.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(64)))));
            this.butKetthuc.Image = global::QuanLyPhongKham.Properties.Resources.butKetthuc1;
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(546, 1);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(78, 31);
            this.butKetthuc.TabIndex = 8;
            this.butKetthuc.Text = "      &Kết thúc";
            this.butKetthuc.UseVisualStyleBackColor = true;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butChon
            // 
            this.butChon.Image = global::QuanLyPhongKham.Properties.Resources.Ok;
            this.butChon.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butChon.Location = new System.Drawing.Point(72, 38);
            this.butChon.Name = "butChon";
            this.butChon.Size = new System.Drawing.Size(83, 31);
            this.butChon.TabIndex = 60;
            this.butChon.Text = "    &Đơn thuốc";
            this.butChon.UseVisualStyleBackColor = true;
            this.butChon.Visible = false;
            // 
            // butHuy
            // 
            this.butHuy.Image = global::QuanLyPhongKham.Properties.Resources.butXoa;
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(389, 1);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(65, 31);
            this.butHuy.TabIndex = 6;
            this.butHuy.Text = "   &Hủy";
            this.butHuy.UseVisualStyleBackColor = true;
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // butSua
            // 
            this.butSua.Image = global::QuanLyPhongKham.Properties.Resources.butSua;
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(127, 1);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(62, 31);
            this.butSua.TabIndex = 4;
            this.butSua.Text = "     &Sửa";
            this.butSua.UseVisualStyleBackColor = true;
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butMoi
            // 
            this.butMoi.Image = global::QuanLyPhongKham.Properties.Resources.butMoi;
            this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butMoi.Location = new System.Drawing.Point(3, 1);
            this.butMoi.Name = "butMoi";
            this.butMoi.Size = new System.Drawing.Size(62, 31);
            this.butMoi.TabIndex = 0;
            this.butMoi.Text = "     &Mới";
            this.butMoi.UseVisualStyleBackColor = true;
            this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
            // 
            // butLuu
            // 
            this.butLuu.Image = global::QuanLyPhongKham.Properties.Resources.butLuu1;
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(65, 1);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(62, 31);
            this.butLuu.TabIndex = 2;
            this.butLuu.Text = "     &Lưu";
            this.butLuu.UseVisualStyleBackColor = true;
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butIn
            // 
            this.butIn.Image = global::QuanLyPhongKham.Properties.Resources.butIn;
            this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butIn.Location = new System.Drawing.Point(454, 1);
            this.butIn.Name = "butIn";
            this.butIn.Size = new System.Drawing.Size(92, 31);
            this.butIn.TabIndex = 7;
            this.butIn.Text = "      &Phiếu nhập";
            this.butIn.UseVisualStyleBackColor = true;
            this.butIn.Click += new System.EventHandler(this.butIn_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Image = global::QuanLyPhongKham.Properties.Resources.butBoqua;
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(314, 1);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(75, 31);
            this.butBoqua.TabIndex = 5;
            this.butBoqua.Text = "     &Bỏ qua";
            this.butBoqua.UseVisualStyleBackColor = true;
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // label3
            // 
            this.label3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.Location = new System.Drawing.Point(471, 6);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(64, 23);
            this.label3.TabIndex = 115;
            this.label3.Text = "Lý do :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // lydo
            // 
            this.lydo.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.lydo.BackColor = System.Drawing.SystemColors.HighlightText;
            this.lydo.Enabled = false;
            this.lydo.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.lydo.Location = new System.Drawing.Point(532, 6);
            this.lydo.Name = "lydo";
            this.lydo.Size = new System.Drawing.Size(95, 21);
            this.lydo.TabIndex = 116;
            this.lydo.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lydo_KeyDown);
            // 
            // butTonghop
            // 
            this.butTonghop.BackColor = System.Drawing.Color.White;
            //this.butTonghop.Image = global::QuanLyPhongKham.Properties.Resources.img_064;
            this.butTonghop.Location = new System.Drawing.Point(300, 5);
            this.butTonghop.Name = "butTonghop";
            this.butTonghop.Size = new System.Drawing.Size(24, 23);
            this.butTonghop.TabIndex = 62;
            this.butTonghop.UseVisualStyleBackColor = false;
            this.butTonghop.Click += new System.EventHandler(this.butTonghop_Click);
            // 
            // losx
            // 
            this.losx.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.losx.BackColor = System.Drawing.SystemColors.HighlightText;
            this.losx.Enabled = false;
            this.losx.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.losx.Location = new System.Drawing.Point(391, 382);
            this.losx.Name = "losx";
            this.losx.Size = new System.Drawing.Size(236, 21);
            this.losx.TabIndex = 117;
            // 
            // handung
            // 
            this.handung.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.handung.BackColor = System.Drawing.SystemColors.HighlightText;
            this.handung.Enabled = false;
            this.handung.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.handung.Location = new System.Drawing.Point(262, 382);
            this.handung.Name = "handung";
            this.handung.Size = new System.Drawing.Size(57, 21);
            this.handung.TabIndex = 118;
            // 
            // soluong
            // 
            this.soluong.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.soluong.BackColor = System.Drawing.SystemColors.HighlightText;
            this.soluong.Enabled = false;
            this.soluong.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.soluong.Location = new System.Drawing.Point(78, 359);
            this.soluong.Name = "soluong";
            this.soluong.Size = new System.Drawing.Size(57, 21);
            this.soluong.TabIndex = 119;
            this.soluong.Validated += new System.EventHandler(this.soluong_Validated);
            this.soluong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.soluong_KeyDown);
            // 
            // dongia
            // 
            this.dongia.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.dongia.BackColor = System.Drawing.SystemColors.HighlightText;
            this.dongia.Enabled = false;
            this.dongia.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dongia.Location = new System.Drawing.Point(203, 359);
            this.dongia.Name = "dongia";
            this.dongia.Size = new System.Drawing.Size(116, 21);
            this.dongia.TabIndex = 120;
            this.dongia.Validated += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            // 
            // giaban
            // 
            this.giaban.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.giaban.BackColor = System.Drawing.SystemColors.HighlightText;
            this.giaban.Enabled = false;
            this.giaban.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.giaban.Location = new System.Drawing.Point(391, 359);
            this.giaban.Name = "giaban";
            this.giaban.Size = new System.Drawing.Size(88, 21);
            this.giaban.TabIndex = 121;
            // 
            // sotien
            // 
            this.sotien.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.sotien.BackColor = System.Drawing.SystemColors.HighlightText;
            this.sotien.Enabled = false;
            this.sotien.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sotien.Location = new System.Drawing.Point(524, 359);
            this.sotien.Name = "sotien";
            this.sotien.Size = new System.Drawing.Size(103, 21);
            this.sotien.TabIndex = 122;
            // 
            // dvt
            // 
            this.dvt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.dvt.BackColor = System.Drawing.SystemColors.HighlightText;
            this.dvt.Enabled = false;
            this.dvt.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dvt.Location = new System.Drawing.Point(570, 336);
            this.dvt.Name = "dvt";
            this.dvt.Size = new System.Drawing.Size(57, 21);
            this.dvt.TabIndex = 123;
            // 
            // sophieu
            // 
            this.sophieu.BackColor = System.Drawing.SystemColors.HighlightText;
            this.sophieu.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this.sophieu.Enabled = false;
            this.sophieu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.sophieu.Location = new System.Drawing.Point(208, 6);
            this.sophieu.Name = "sophieu";
            this.sophieu.Size = new System.Drawing.Size(91, 21);
            this.sophieu.TabIndex = 124;
            this.sophieu.Validated += new System.EventHandler(this.sophieu_Validated);
            this.sophieu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.sophieu_KeyDown);
            // 
            // ngayhd
            // 
            this.ngayhd.BackColor = System.Drawing.Color.White;
            this.ngayhd.Enabled = false;
            this.ngayhd.Location = new System.Drawing.Point(78, 7);
            this.ngayhd.Mask = "00/00/0000";
            this.ngayhd.Name = "ngayhd";
            this.ngayhd.Size = new System.Drawing.Size(64, 20);
            this.ngayhd.TabIndex = 125;
            this.ngayhd.ValidatingType = typeof(System.DateTime);
            this.ngayhd.Validated += new System.EventHandler(this.ngayhd_Validated);
            // 
            // frmHoantrancc
            // 
            //this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(628, 470);
            this.Controls.Add(this.ngayhd);
            this.Controls.Add(this.dvt);
            this.Controls.Add(this.sotien);
            this.Controls.Add(this.giaban);
            this.Controls.Add(this.dongia);
            this.Controls.Add(this.soluong);
            this.Controls.Add(this.handung);
            this.Controls.Add(this.losx);
            this.Controls.Add(this.butTonghop);
            this.Controls.Add(this.lydo);
            this.Controls.Add(this.bNut);
            this.Controls.Add(this.dataGrid1);
            this.Controls.Add(this.khox);
            this.Controls.Add(this.tendv);
            this.Controls.Add(this.madv);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.timkiem);
            this.Controls.Add(this.cmbSophieu);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.manguon);
            this.Controls.Add(this.tenbd);
            this.Controls.Add(this.tenhc);
            this.Controls.Add(this.lTenhc);
            this.Controls.Add(this.label24);
            this.Controls.Add(this.label15);
            this.Controls.Add(this.mabd);
            this.Controls.Add(this.label20);
            this.Controls.Add(this.label18);
            this.Controls.Add(this.label17);
            this.Controls.Add(this.label16);
            this.Controls.Add(this.ldvt);
            this.Controls.Add(this.lTen);
            this.Controls.Add(this.label13);
            this.Controls.Add(this.listDMBD);
            this.Controls.Add(this.stt);
            this.Controls.Add(this.sttt);
            this.Controls.Add(this.label25);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.sophieu);
            this.Controls.Add(this.label1);
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "frmHoantrancc";
            //this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Load += new System.EventHandler(this.frmXhtncc_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.bNut.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmXhtncc_Load(object sender, System.EventArgs e)
		{
            user = d.user; 
			manguon.DisplayMember="TEN";
			manguon.ValueMember="ID";
			manguon.DataSource=d.get_data("select * from "+user+".d_dmnguon order by id").Tables[0];
            //
			khox.DisplayMember="TEN";
			khox.ValueMember="ID";
            sql = "select * from " + user + ".d_dmkho order by id";
			khox.DataSource=d.get_data(sql).Tables[0];
            dtdmnx = d.get_data("select * from " + user + ".d_dmnx order by id").Tables[0];
            //load_dm();
			cmbSophieu.DisplayMember="SOPHIEU";
			cmbSophieu.ValueMember="ID";
            load_sophieu();
            l_id = (cmbSophieu.Items.Count > 0) ? long.Parse(cmbSophieu.SelectedValue.ToString()) : 0;
            load_head(); load_grid();
			AddGridTableStyle();
            cmbSophieu.Focus();
		}
        private void load_sophieu()
        {
            sql = "select id,sophieu,to_char(ngay,'dd/mm/yyyy') as ngay,khox,lydo,khon,idduyet from " + user + ".d_xuatll where loai='" + s_loai + "'";
            sql += " and userid=" + i_userid + " order by id";
            dtll = d.get_data(sql).Tables[0];
            cmbSophieu.DataSource = dtll;
        }
		private void load_grid()
		{
            sql = "select a.stt,a.sttt,a.mabd,b.ma,trim(b.ten)||' '||b.hamluong as ten,b.tenhc,b.dang,a.madv,a.manguon,a.handung,a.losx,a.soluong,a.giaban,a.giamua,round(a.soluong*a.giamua,3) as sotien ";
			sql+=" from "+user+".d_xuatct a,"+user+".d_dmbd b";
            sql+=" where a.mabd=b.id and a.id="+l_id+" order by a.stt";
			dtct=d.get_data(sql).Tables[0];
			dataGrid1.DataSource=dtct;
            dtxoa = dtct.Clone();
		}
		private void ref_text()
		{
			try
			{
				int i=dataGrid1.CurrentCell.RowNumber;
				stt.Text=dataGrid1[i,0].ToString();
				mabd.Text=dataGrid1[i,1].ToString();
				tenbd.Text=dataGrid1[i,2].ToString();
				tenhc.Text=dataGrid1[i,3].ToString();
				dvt.Text=dataGrid1[i,4].ToString();
				handung.Text=dataGrid1[i,5].ToString();
				losx.Text=dataGrid1[i,6].ToString();
				d_soluong=(dataGrid1[i,7].ToString()!="")?decimal.Parse(dataGrid1[i,7].ToString()):0;
				d_dongia=(dataGrid1[i,8].ToString()!="")?decimal.Parse(dataGrid1[i,8].ToString()):0;
				d_sotien=(dataGrid1[i,9].ToString()!="")?decimal.Parse(dataGrid1[i,9].ToString()):0;
				d_giaban=(dataGrid1[i,10].ToString()!="")?decimal.Parse(dataGrid1[i,10].ToString()):0;
                soluong.Text = d_soluong.ToString("#,###,###,##0.0");
                dongia.Text = d_dongia.ToString("#,###,###,##0.00");
                sotien.Text = d_sotien.ToString("#,###,###,##0.00");
                giaban.Text = d_giaban.ToString("#,###,###,##0.00");
				sttt.Text=dataGrid1[i,11].ToString();
				d_soluongcu=d_soluong;
			}
			catch{}
		}
		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = dtct.TableName;
            ts.AlternatingBackColor = Color.Linen;
            ts.BackColor = Color.GhostWhite;
            ts.ForeColor = Color.MidnightBlue;
            ts.GridLineColor = SystemColors.Control;
            ts.HeaderBackColor = SystemColors.Control;
            ts.HeaderForeColor = Color.Navy;
            ts.SelectionBackColor = Color.Teal;
            ts.SelectionForeColor = Color.White;
            ts.RowHeaderWidth = 5;
            ts.AllowSorting = false;
						
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stt";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ma";
			TextCol.HeaderText = "Mã số";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ten";
			TextCol.HeaderText = "Tên";
			TextCol.Width = 300;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tenhc";
			TextCol.HeaderText = "Hoạt chất";
			TextCol.Width = 200;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "dang";
			TextCol.HeaderText = "ĐVT";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "handung";
			TextCol.HeaderText = "Date";
			TextCol.Width = 40;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "losx";
			TextCol.HeaderText = "Lô SX";
			TextCol.Width = 80;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "soluong";
			TextCol.HeaderText = "Số lượng";
			TextCol.Width = 80;
			TextCol.Format="###,###,##0.0";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "giamua";
			TextCol.HeaderText = "Đơn giá";
			TextCol.Width = 100;
            TextCol.Format = "###,###,##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sotien";
			TextCol.HeaderText = "Số tiền";
			TextCol.Width = 100;
            TextCol.Format = "###,###,##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "giaban";
			TextCol.HeaderText = "Giá bán";
			TextCol.Width = 100;
            TextCol.Format = "###,###,##0.00";
			TextCol.Alignment=HorizontalAlignment.Right;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sttt";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}
		private void sophieu_Validated(object sender, System.EventArgs e)
		{
			try
			{
                l_idduyet = 0;
				r=d.getrowbyid(dtll,"sophieu='"+sophieu.Text+"'");
				if (r!=null)
				{
                    MessageBox.Show("Hoá đơn này đã trả !", DAL.AccessData.Msg);
					sophieu.Focus();
				}
                get_hoadon();
			}
			catch{}
		}
        private void get_hoadon()
        {
            sql = "select b.sttn as stt,c.ma,trim(c.ten)||' '||c.hamluong as ten,c.tenhc,c.dang,";
            sql += " b.handung,b.losx,round(b.slnhap,1) as soluong,round(b.giamua,2) as giamua,round(b.slnhap*b.giamua,2) as sotien,round(b.giaban,2) as giaban,b.madv,a.manguon,a.makho,a.id,b.stt as sttt,b.mabd";
            sql += " from " + user + ".d_nhapll_" + d.mmyy(ngayhd.Text) + " a," + user + ".d_tonkhoct_" + d.mmyy(ngayhd.Text) + " b," + user + ".d_dmbd c";
            sql += " where a.id=b.idn and b.mabd=c.id and b.slxuat=0 and a.sohd='"+sophieu.Text.Trim()+"' order by b.sttn";
            dtton = d.get_data(sql).Tables[0];
            listDMBD.DataSource = dtton;
            if (!bEdit)
            {
                foreach (DataRow rr in dtton.Rows)
                {
                    khox.SelectedValue = rr["makho"].ToString();
                    madv.Text = rr["madv"].ToString();
                    tendv.Text = d.getrowbyid(dtdmnx, "id=" + int.Parse(madv.Text))["ten"].ToString();
                    l_idduyet = long.Parse(rr["id"].ToString()); break;
                }
            }
        }
		private void cmbSophieu_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab) butMoi.Focus();
		}

		private void cmbSophieu_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==cmbSophieu)
			{
				try
				{
					l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
				}
				catch{l_id=0;}
                load_head(); load_grid();
			}
		}

		private void load_head()
		{
			try
			{
				r=d.getrowbyid(dtll,"id="+l_id);
				if (r!=null)
				{
                    lydo.Text = r["lydo"].ToString();
                    l_idduyet=long.Parse(r["idduyet"].ToString());
					sophieu.Text=r["sophieu"].ToString();
					ngayhd.Text=r["ngay"].ToString();
					khox.SelectedValue=r["khox"].ToString();
					s_ngaysp=ngayhd.Text;
					DataRow r1=d.getrowbyid(dtdmnx,"id="+int.Parse(r["khon"].ToString()));
					if (r1!=null)
					{
						madv.Text=r1["id"].ToString();
						tendv.Text=r1["ten"].ToString();
					}
				}
			}
			catch{l_id=0;}
			load_grid();
			ref_text();
		}
		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			//this.Close();
		}

		private void Filter_dmbd(string ten)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[listDMBD.DataSource];
				DataView dv=(DataView)cm.List;
				sql="ten like '%"+ten.Trim()+"%' or tenhc like '%"+ten.Trim()+"%'";
				dv.RowFilter=sql;
			}
			catch{}
		}

		private void tenbd_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listDMBD.Focus();
			else if (e.KeyCode==Keys.Enter)
			{
				if (listDMBD.Visible) listDMBD.Focus();
				else SendKeys.Send("{Tab}");
			}
		}

		private void tenbd_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==tenbd && dtton.Rows.Count>0)
			{
				if (butMoi.Enabled) return;
				Filter_dmbd(tenbd.Text);
                listDMBD.Tonkhoct_ncc(tenbd, mabd, soluong, mabd.Location.X, mabd.Location.Y + mabd.Height - 2, mabd.Width + lTen.Width + tenbd.Width + lTenhc.Width + tenhc.Width + ldvt.Width + dvt.Width + 20, mabd.Height + 5);
			}
		}

		private void tenbd_Validated(object sender, System.EventArgs e)
		{
			if(!listDMBD.Focused) listDMBD.Hide();
		}

		private void ena_object(bool ena)
		{
			timkiem.Enabled=!ena;
			sophieu.Visible=ena;
			cmbSophieu.Visible=!ena;
            sophieu.Enabled = ena;
            //ngayhd.Enabled=ena;
            lydo.Enabled = ena;
			mabd.Enabled=ena;
			tenbd.Enabled=ena;
			soluong.Enabled=ena;
			butMoi.Enabled=!ena;
			butSua.Enabled=!ena;
			butXoa.Enabled=ena;
			butLuu.Enabled=ena;
			butBoqua.Enabled=ena;
			butHuy.Enabled=!ena;
			butIn.Enabled=!ena;
			butKetthuc.Enabled=!ena;
        }

		private void emp_head()
		{
			l_id=0;
			sophieu.Text="";
			ngayhd.Text=s_ngay;
			madv.Text="";tendv.Text="";
			khox.SelectedIndex=0;
			s_ngaysp=ngayhd.Text;
			dtxoa.Clear();
		}
		
		private void emp_detail()
		{
			sttt.Text="";
			stt.Text="";
			mabd.Text="";
			tenbd.Text="";
			tenhc.Text="";
			dvt.Text="";
			soluong.Text="0";
			dongia.Text="0";
			sotien.Text="0";
			handung.Text="";
			losx.Text="";
			giaban.Text="0";
			d_soluongcu=0;
		}
		private void butMoi_Click(object sender, System.EventArgs e)
		{
			if (d.bKhoaso(s_mmyy))
			{
				MessageBox.Show("Số liệu tháng"+s_mmyy.Substring(0,2)+" năm "+s_mmyy.Substring(2,2)+" đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống",DAL.AccessData.Msg);
				return;
			}
			ena_object(true);
			dtct.Clear();
			emp_head();
			emp_detail();
			bNew=true;
			bEdit=false;
			sophieu.Focus();
		}
		private void butSua_Click(object sender, System.EventArgs e)
		{
			if (cmbSophieu.Items.Count==0) return;
            if (d.bKhoaso(s_mmyy))
            {
                MessageBox.Show("Số liệu tháng" + s_mmyy.Substring(0, 2) + " năm " + s_mmyy.Substring(2, 2) + " đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống", DAL.AccessData.Msg);
                return;
            }
			l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
            if (dtton.Rows.Count == 0) get_hoadon();
			ena_object(true);
			bNew=false;
			bEdit=true;
			dtxoa.Clear();
			lydo.Focus();
			ref_text();
		}
		private bool KiemtraDetail()
		{
			i_mabd=0;
			if (mabd.Text=="" || tenbd.Text=="")
			{
				tenbd.Focus();
				return false;
			}
            if (dtton.Rows.Count > 0)
            {
                r = d.getrowbyid(dtton, "ma='" + mabd.Text + "'");
                if (r == null)
                {
                    MessageBox.Show("Mã số không hợp lệ !", DAL.AccessData.Msg);
                    tenbd.Focus();
                    return false;
                }
                i_mabd = int.Parse(r["mabd"].ToString());
            }
			if (soluong.Text=="" || soluong.Text=="0.00" || soluong.Text=="0")
			{
				MessageBox.Show("Nhập số lượng !",DAL.AccessData.Msg);
				soluong.Focus();
				return false;
			}
			return true;
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			bEdit=false;
			if (cmbSophieu.Items.Count>0) l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
			else l_id=0;
			load_head();
			ena_object(false);
			butMoi.Focus();
		}

		private void ngaysp_Validated(object sender, System.EventArgs e)
		{
            //SendKeys.Send("{F4}");
		}

		private void butXoa_Click(object sender, System.EventArgs e)
		{
			if (!upd_table(dtxoa,true)) return;
			d.delrec(dtct,"stt="+int.Parse(stt.Text));
			dtct.AcceptChanges();
			if (dtct.Rows.Count==0) emp_detail();
			else ref_text();
		}

		private bool upd_table(DataTable dt,bool del)
		{
			if (!KiemtraDetail()) return false;
			d_soluong=(soluong.Text!="")?decimal.Parse(soluong.Text):0;
			d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
			d_sotien=(sotien.Text!="")?decimal.Parse(sotien.Text):0;
			d_giaban=(giaban.Text!="")?decimal.Parse(giaban.Text):0;
			l_sttt=(sttt.Text!="")?long.Parse(sttt.Text):0;
			d.updrec_xuatct(dt,int.Parse(stt.Text),l_sttt,i_mabd,int.Parse(madv.Text),int.Parse(manguon.SelectedValue.ToString()),int.Parse(khox.SelectedValue.ToString()), mabd.Text,tenbd.Text,tenhc.Text,dvt.Text,handung.Text,losx.Text,d_soluong,d_dongia,d_sotien,d_giaban,(del)?null:dtton);
			return true;
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			ref_text();
		}

		private void tinh_giatri()
		{
			try
			{
				d_soluong=(soluong.Text!="")?decimal.Parse(soluong.Text):0;
				d_dongia=(dongia.Text!="")?decimal.Parse(dongia.Text):0;
				d_sotien=Math.Round(d_dongia*d_soluong,3);
				sotien.Text=d_sotien.ToString("###,###,##0.00");
			}
			catch{}
		}

		private void soluong_Validated(object sender, System.EventArgs e)
		{
			try
			{
				d_soluong=(soluong.Text!="")?decimal.Parse(soluong.Text):0;
                soluong.Text = d_soluong.ToString("###,###,##0.0");
				if (mabd.Text!="" && tenbd.Text!="")
				{
					r=d.getrowbyid(dtton,"ma='"+mabd.Text+"' and sttt="+l_sttt);
					if (r!=null)
					{
                        d_soluongton = decimal.Parse(r["soluong"].ToString());
						if (d_soluong>d_soluongton)
						{
                            MessageBox.Show("Số lượng hoàn trả lớn hơn số lượng nhập !(" + d_soluongton.ToString() + ")", DAL.AccessData.Msg);
							soluong.Focus();
							return;
						}
					}
				}
			}
			catch{}
			tinh_giatri();
            upd_table(dtct, true);
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			try
			{
				if (cmbSophieu.Items.Count==0) return;
				if (d.bKhoaso(s_mmyy))
				{
					MessageBox.Show("Số liệu tháng"+s_mmyy.Substring(0,2)+" năm "+s_mmyy.Substring(2,2)+" đã khóa !\nNếu cần thay đổi thì vào mục khai báo hệ thống",DAL.AccessData.Msg);
					return;
				}
				if (MessageBox.Show("Đồng ý hủy số phiếu này ?",DAL.AccessData.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
				{
					l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
                    foreach (DataRow r1 in dtct.Rows)
                    {
                        d.upd_theodoisua(d.get_capid_theodoisua, sophieu.Text, l_id, int.Parse(r1["mabd"].ToString()), r1["ten"].ToString(), decimal.Parse(r1["soluong"].ToString()), i_userid, "HT", "d_xuatct", 0, 0, 1);
                        d.upd_tonkhoct_xuat("delete", s_mmyy, long.Parse(r1["sttt"].ToString()), int.Parse(khox.SelectedValue.ToString()),int.Parse(manguon.SelectedValue.ToString()),int.Parse(r1["mabd"].ToString()), r1["handung"].ToString(), r1["losx"].ToString(), int.Parse(madv.Text), decimal.Parse(r1["soluong"].ToString()), decimal.Parse(r1["sotien"].ToString()), decimal.Parse(r1["giaban"].ToString()), decimal.Parse(r1["giamua"].ToString()));
                    }
					d.execute_data("delete from " + user + ".d_xuatct where id="+l_id);
                    d.execute_data("delete from " + user + ".d_xuatll where id=" + l_id);
					d.delrec(dtll,"id="+l_id);
					cmbSophieu.Refresh();
					if (cmbSophieu.Items.Count>0) l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
					else l_id=0;
					load_head();
				}
			}
			catch{}
		}

		private void khox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				if (khox.SelectedIndex==-1) khox.SelectedIndex=0;
				SendKeys.Send("{Tab}{F4}");	
			}
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
            if (sophieu.Text.Trim() == "")
            {
                sophieu.Focus(); return;
            }
			bEdit=false;
			upd_table(dtct,true);
            if (dtct.Rows.Count == 0) return;
			dtct.AcceptChanges();
            l_id = (bNew) ? d.get_capid : l_id;
			if (!d.upd_xuatll(s_mmyy,l_id,sophieu.Text,s_ngay,s_loai,int.Parse(khox.SelectedValue.ToString()),int.Parse(madv.Text),lydo.Text,i_userid,l_idduyet))
			{
                MessageBox.Show("Không cập nhật được thông tin phiếu hoàn trả !", DAL.AccessData.Msg);
				return;
			}
            if (!bNew && bEdit)
            {
                foreach (DataRow r1 in dtxoa.Rows)
                {
                    d.execute_data("delete from " + user + ".d_xuatct where id=" + l_id + " and stt=" + long.Parse(r1["stt"].ToString()));
                    d.upd_tonkhoct_xuat("delete", s_mmyy, long.Parse(r1["sttt"].ToString()),int.Parse(khox.SelectedValue.ToString()), int.Parse(manguon.SelectedValue.ToString()), int.Parse(r1["mabd"].ToString()), r1["handung"].ToString(), r1["losx"].ToString(), int.Parse(madv.Text), decimal.Parse(r1["soluong"].ToString()), decimal.Parse(r1["sotien"].ToString()), decimal.Parse(r1["giaban"].ToString()), decimal.Parse(r1["giamua"].ToString()));
                    d.upd_theodoisua(d.get_capid_theodoisua, sophieu.Text, l_id, int.Parse(r1["mabd"].ToString()), r1["ten"].ToString(), decimal.Parse(r1["soluong"].ToString()), i_userid, "HT", "d_xuatct", 0, 1, 0);
                }
            }
			foreach(DataRow r1 in dtct.Rows)
			{
				d.upd_xuatct(l_id,int.Parse(r1["stt"].ToString()),long.Parse(r1["sttt"].ToString()),int.Parse(r1["mabd"].ToString()),int.Parse(madv.Text),int.Parse(manguon.SelectedValue.ToString()),decimal.Parse(r1["soluong"].ToString()),r1["handung"].ToString(),r1["losx"].ToString(),decimal.Parse(r1["giamua"].ToString()),decimal.Parse(r1["giaban"].ToString()));
				d.upd_tonkhoct_xuat("insert",s_mmyy,long.Parse(r1["sttt"].ToString()),int.Parse(khox.SelectedValue.ToString()),int.Parse(manguon.SelectedValue.ToString()),int.Parse(r1["mabd"].ToString()),r1["handung"].ToString(),r1["losx"].ToString(),int.Parse(madv.Text),decimal.Parse(r1["soluong"].ToString()),decimal.Parse(r1["sotien"].ToString()),decimal.Parse(r1["giaban"].ToString()),decimal.Parse(r1["giamua"].ToString()));
			}
			d.updrec_xuatll(dtll,l_id,sophieu.Text,ngayhd.Text,int.Parse(khox.SelectedValue.ToString()),int.Parse(madv.Text),lydo.Text);
			try
			{
				if (dtll.Rows.Count>0) cmbSophieu.SelectedIndex=dtll.Rows.Count-1;
			}
			catch{}
			if (cmbSophieu.Items.Count>0) load_head();
			ena_object(false);
			butMoi.Focus();
		}

		private void tongcong(DataTable dt)
		{
			d_tongcong=0;
			foreach(DataRow r1 in dt.Rows) d_tongcong+=decimal.Parse(r1["sotien"].ToString());
		}

		private void butIn_Click(object sender, System.EventArgs e)
		{
			if (dtct.Rows.Count==0) return;
			DataTable dttmp=dtct.Copy();
			tongcong(dtct);
			string tenfile="d_phieuhoantrancc.rpt";
			frmReport f=new frmReport(d,dtct,"",tenfile,cmbSophieu.Text,ngayhd.Text,"","",tendv.Text,"Hoàn trả",khox.Text,"","","","","");
			//f.ShowDialog();
            this.FormMain.OpenTab("Báo cáo", f);
		}
		private void mabd_TextChanged(object sender, System.EventArgs e)
		{
            if (this.ActiveControl == mabd && dtton.Rows.Count > 0)
            {
                if (butMoi.Enabled) return;
                Filter_mabd(mabd.Text);
                listDMBD.Tonkhoct_ncc(mabd, tenbd, soluong, mabd.Location.X, mabd.Location.Y + mabd.Height - 2, mabd.Width + lTen.Width + tenbd.Width + lTenhc.Width + tenhc.Width + ldvt.Width + dvt.Width + 20, mabd.Height + 5);
            }
		}

		private void Filter_mabd(string ma)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[listDMBD.DataSource];
				DataView dv=(DataView)cm.List;
				sql="ma like '%"+ma.Trim()+"%'";
				dv.RowFilter=sql;
			}
			catch{}
		}

		private void get_items(int stt)
		{
			try
			{
				r=d.getrowbyid(dtton,"soluong>0 and stt="+stt);
				if (r!=null)
				{
					mabd.Text=r["ma"].ToString();
					tenbd.Text=r["ten"].ToString();
					tenhc.Text=r["tenhc"].ToString();
					dvt.Text=r["dang"].ToString();
					manguon.SelectedValue=r["manguon"].ToString();
					sttt.Text=r["sttt"].ToString();
					handung.Text=r["handung"].ToString();
					losx.Text=r["losx"].ToString();
					d_dongia=decimal.Parse(r["giamua"].ToString());
					d_giaban=decimal.Parse(r["giaban"].ToString());
                    dongia.Text = d_dongia.ToString("#,###,###,##0.00");
                    giaban.Text = d_giaban.ToString("#,###,###,##0.00");
					listDMBD.Hide();
					soluong.Focus();
				}
			}
			catch{}		
		}

		private void listDMBD_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter)
			{
				try
				{
					r=d.getrowbyid(dtton,"soluong>0 and stt="+int.Parse(mabd.Text));
					if (r!=null)
					{
						mabd.Text=r["ma"].ToString();
						tenbd.Text=r["ten"].ToString();
						tenhc.Text=r["tenhc"].ToString();
						dvt.Text=r["dang"].ToString();
						manguon.SelectedValue=r["manguon"].ToString();
						sttt.Text=r["sttt"].ToString();
						handung.Text=r["handung"].ToString();
						losx.Text=r["losx"].ToString();
						d_dongia=decimal.Parse(r["giamua"].ToString());
						d_giaban=decimal.Parse(r["giaban"].ToString());
                        dongia.Text = d_dongia.ToString("#,###,###,##0.00");
                        giaban.Text = d_giaban.ToString("#,###,###,##0.00");
						listDMBD.Hide();
						soluong.Focus();
					}
				}
				catch{}		
			}
		}

		private void mabd_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listDMBD.Focus();
			else if (e.KeyCode==Keys.Enter)
			{
				sql="ma like '"+mabd.Text.Trim()+"%'";
				DataRow [] dr=dtton.Select(sql);
				if (dr.Length==1)
				{
					mabd.Text=dr[0]["stt"].ToString();
					get_items(int.Parse(mabd.Text));
					if(!listDMBD.Focused) listDMBD.Hide();
					soluong.Focus();
				}
				else
				{
					if (listDMBD.Visible)
					{
						listDMBD.Focus();
						SendKeys.Send("{Up}");
					}
					else SendKeys.Send("{Tab}");
				}
			}
		}

		private void find_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==timkiem) RefreshChildren(timkiem.Text);
		}

		private void RefreshChildren(string text)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[cmbSophieu.DataSource];
				DataView dv=(DataView)cm.List;			
				dv.RowFilter="sophieu like '%"+text.Trim()+"%'";
				if(cmbSophieu.SelectedIndex>=0)	l_id=long.Parse(cmbSophieu.SelectedValue.ToString());
				else l_id=0;
				load_head();
			}
			catch(Exception ex){MessageBox.Show(ex.Message);}
		}

		private void find_Enter(object sender, System.EventArgs e)
		{
			timkiem.Text="";
		}
		private void madv_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

        private void bNut_SizeChanged(object sender, EventArgs e)
        {
            bNut.Location = new Point((this.Width - 628) / 2 + 1, (this.Height - 470) + 433);
        }

        private void butTonghop_Click(object sender, EventArgs e)
        {
            foreach(DataRow r1 in dtton.Rows)
            {
                d.updrec_xuatct(dtct, int.Parse(r1["stt"].ToString()), long.Parse(r1["sttt"].ToString()), int.Parse(r1["mabd"].ToString()), int.Parse(r1["madv"].ToString()), int.Parse(r1["manguon"].ToString()),
                    int.Parse(r1["makho"].ToString()), r1["ma"].ToString(), r1["ten"].ToString(), r1["tenhc"].ToString(), r1["dang"].ToString(), r1["handung"].ToString(), r1["losx"].ToString(), decimal.Parse(r1["soluong"].ToString()), decimal.Parse(r1["giamua"].ToString()),decimal.Parse(r1["sotien"].ToString()), decimal.Parse(r1["giaban"].ToString()), dtton);
            }
            dtct.AcceptChanges(); dataGrid1.Refresh(); ref_text(); lydo.Focus();
        }

        private void sophieu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                butTonghop.Focus();
            }
        }

        private void soluong_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                butLuu.Focus();
            }
        }

        private void lydo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                butLuu.Focus();
            }
        }

        private void butThem_Click(object sender, EventArgs e)
        {
            if (!upd_table(dtct, true)) return;
            dtct.AcceptChanges();
            emp_detail();
            tenbd.Focus();
        }

        private void ngayhd_Validated(object sender, EventArgs e)
        {
            if (!d.bNgay(ngayhd.Text.Trim()))
            {
                MessageBox.Show("Ngày không hợp lệ! Vui lòng nhập lại theo định dạng : dd/MM/yyyy", DAL.AccessData.Msg);
                ngayhd.Focus();
                return;
            }
        }
	}
}
